// index.js

const {
  baseUrl
} = require("../../config/api");
var app = getApp()

// js文件
Page({
  data: {
    tabValu: 1,
    dataList: {},
    userInfo: null,
    tabList: [{
      label: '全部业务',
      type: 1
    }, {
      label: '处理中',
      type: 2
    }, {
      label: '已完成',
      type: 3
    }, {
      label: '失败',
      type: 4
    }],
    fabButton: {
      icon: 'share',
      openType: 'share',
    },
    // 轮播图 list
    swiperList: [],
    // 轮播通知 list
    noticeList: [],
  },

  onShow() {
    this.getTabBar().init();
    let info = wx.getStorageSync("userInfo")
    if (info === '' || info === undefined) {
      console.log("[User not login]");
      return
    }
    let userInfo = JSON.parse(info)
    this.setData({
      userInfo: userInfo
    })

    let _this = this
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: baseUrl + 'sys/worksheetRecord/wx/list/' + userInfo.userId,
      method: 'GET',
      success: (res) => {
        wx.hideLoading()
        // console.log(res)
        if (res.data.code === 0) {
          _this.setData({
            dataList: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'error',
            duration: 2000
          });
        }
      }
    })
    this.initTypeList();
    this.getHomepagePush();
  },
  initTypeList() {
    wx.showLoading({
      title: '加载中',
    })
    let _this = this
    wx.request({
      url: baseUrl + 'sys/worksheet/type/tree',
      method: 'get',
      success(res) {
        console.log("request tree suc , res is ", res.data.data);
        if (res.data.code === 0) {
          app.globalData.typeList = JSON.parse(JSON.stringify(res.data.data));
          let map = {}
          res.data.data.forEach(element => {
            let str = element.label
            element.children.forEach(item =>{
              map[item.value] = str+" - "+item.label
            })
          });
          _this.setData({
            typeMap:map
          })          
          // 保存 typeMap  
          app.globalData.typeMap = map
        }
        wx.hideLoading()
      }
    })
  },
  onLoad() {
    this.getTabBar().init();
    this.getHomepagePush();
  },
  handleClick(e) {
    let info = wx.getStorageSync("userInfo")

    if(info == ""){
      wx.showToast({
        title: '请先登录',
        icon: 'error',
      })
      return;
    }
    // 再次获取工单类型
    if(app.globalData.typeList == null){
      console.log('typelist is null, query angain')
      this.initTypeList();
    }
    
    wx.navigateTo({
      url: '/pages/add/addApprove'
    })
  },

  getHomepagePush(){
    // console.log("A");
    let that = this;
    wx.request({
      url: baseUrl+`sys/homepagePush/list?page=1&size=99`,
      method: "GET",
      success(res){
        // console.log(res);
        let list = res.data.data.list;
        const notices =  list.map(item => item.content);
        const images =  list.map(item => (item.imageUrl));
        that.setData({
          noticeList:notices,
          swiperList:images
        });
      },
      fail(){
        wx.showToast({
          title: '获取首页推送数据失败',
          icon: 'error'
        })
      }
    })
  },

})